package org.jimmy.tang.ssl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.Date;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

/**
 * @author jimmy
 * 
 */
public class OneWayHttpsConnectionDemo {

    // 客户端信任的证书
    private String sslTrustStore;
    private String sslTrustStorePassword;
    private String Url;

    // 初始化数据
    public OneWayHttpsConnectionDemo() {
        sslTrustStore = "D:/ssl/clientTrust.jks";
        sslTrustStorePassword = "123456";
        Url = "https://test.yihaodian.com:8443/ims/feedbackToPingAn_getData.action";
    }

    public String sendData(String data) {
        String receivedData = null;
        try {
            // 设置系统参数
            System.setProperty("javax.net.ssl.trustStore", sslTrustStore);
            System.setProperty("javax.net.ssl.trustStorePassword",
                    sslTrustStorePassword);
            receivedData = send(Url, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return receivedData;
    }

    public static String send(String sendurl, String sendData) throws Exception {
        URL url = new URL(sendurl);
        HostnameVerifier hv = new HostnameVerifier() {
            @Override
            public boolean verify(String urlHostName, SSLSession session) {
                return true;
            }
        };
        System.setProperty("java.protocol.handler.pkgs", "sun.net.www.protocol");
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
        Date current = new Date(System.currentTimeMillis());
        System.out.println("begint to open connection at " + current);
        HttpsURLConnection connection = (HttpsURLConnection) url
                .openConnection();
        Date end = new Date(System.currentTimeMillis());
        System.out.println("open connection ok at " + end + ",cost:"
                + (end.getTime() - current.getTime()));
        connection.setRequestProperty("Content-Type", "text/xml");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setReadTimeout(30000);
        byte data[] = sendData.getBytes();
        current = new Date(System.currentTimeMillis());
        System.out.println("[SSLIX]notifyEai,begint to write data at "
                + current);
        OutputStream out = connection.getOutputStream();
        out.write(data);
        end = new Date(System.currentTimeMillis());
        System.out.println("write data ok at " + end + ",cost:"
                + (end.getTime() - current.getTime()));
        StringBuffer receivedData = new StringBuffer();
        current = new Date(System.currentTimeMillis());
        System.out.println("begint to read data at " + current);
        InputStreamReader inReader = new InputStreamReader(
                connection.getInputStream(), "UTF-8");
        BufferedReader aReader = new BufferedReader(inReader);
        String aLine;
        while ((aLine = aReader.readLine()) != null) {
            receivedData.append(aLine);
        }
        end = new Date(System.currentTimeMillis());
        System.out.println("read data ok at " + end + ",cost:"
                + (end.getTime() - current.getTime()));

        System.out.println("开始返回状态码");
        Integer statusCode = connection.getResponseCode();
        System.out.println("返回状态码:" + statusCode);
        aReader.close();
        connection.disconnect();
        return receivedData.toString();
    }

    public static void main(String[] args) {
        OneWayHttpsConnectionDemo t = new OneWayHttpsConnectionDemo();
        t.sendData("测试SSL单项连接，向服务端发送数据!");
    }
}
